<?php

function fetch_sorted_user_results($show, $sort_field) {
  // Make the query.
  if ($sort_field == 'name') {
    $sf = 'last_name, first_name';
  } else {
    $sf = $sort_field;
  }
  $sh=get_show_condition($show);

  $query = "SELECT user_id, first_name, last_name, email, election_year, stop_year, is_current, admin_email_ok, is_committee FROM user_details WHERE  is_active = 1 $sh ORDER BY $sf";
  $result = mysql_query ($query);
  return $result;
}

function fetch_export_details($show) {
  $sh=get_show_condition($show);

  $query = "SELECT first_name, last_name, title, address1, address2, address3, address4, post_code, home_phone, work_phone, mobile_phone, email, email_allowed, election_year, stop_year, is_current, admin_email_ok, is_committee FROM user_details WHERE is_active = 1 $sh";
  $result = mysql_query ($query);
  return $result;
}

function get_show_condition($show) {
  $sh='';
  if ($show == 'comm') {
    $sh = "AND is_committee = 1";
  } else if ($show == 'email') {
    $sh = "AND admin_email_ok = 1";
  } else if ($show == 'noemail') {
    $sh = "AND admin_email_ok = 0";
  } else if ($show == 'current') {
    $sh = "AND is_current = 1";
  } else if ($show == 'ex') {
    $sh = "AND is_current = 0";
  }
  return $sh;
}

function echo_boolean($test, $true_text, $false_text) {
  if ($test) {
    echo $true_text;
  } else {
    echo $false_text;
  }
}

function fetch_active_user_results($get_active) {
  if ($get_active) {
    $a = 1;
  } else {
    $a = 0;
  }
  $query = "SELECT user_id, first_name, last_name FROM user_details WHERE is_active=$a ORDER BY last_name, first_name";
  $result = mysql_query ($query);
  return $result;
}

?>